home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.amiga.programmer
- Path: news.chalmers.se!sunic!EU.net!howland.reston.ans.net!gatech!concert!sas!mozart.unx.sas.com!walker
- From: walker@twix.unx.sas.com (Doug Walker)
- Subject: Re: Allocating Mem & Loading
- Originator: walker@twix.unx.sas.com
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Message-ID: <CrC6F6.KM3@unx.sas.com>
- Date: Mon, 13 Jun 1994 12:52:17 GMT
- References: <2tdfm9$pvd@dsm6.dsmnet.com>
- Nntp-Posting-Host: twix.unx.sas.com
- Organization: SAS Institute Inc.
- Lines: 79
-
-
- In article <2tdfm9$pvd@dsm6.dsmnet.com>, mikebockert@dsm1.dsmnet.com writes:
- |> I would like to allocate memory for an array of a structure. The number of the
- |> array will be deterimined by my data file, so it is not the same everytime.
- |> Well the question is how can I load the entire file into this array without
- |> having a loop reading in one set of the array at a time. I want to do one read
- |> statement to load the entire file into the array of strutures..
- |>
- |> Example:
- |>
- |> long bytes,value;
- |>
- |> bytes = FileSize(file);
- |> value = bytes / sizeof struct(A);
- |>
- |> - then allocate the memory for however many array's we need (Value) and then
- |> I want to do a single read command to load the entire file into that array
- |>
- |> Does anyone know how I could do this? thanks
-
- This is somewhat SAS/C dependent, but it might work with the new
- commercial DICE as well (I think it has the __aligned keyword):
-
- #include <proto/dos.h>
- #include <stdio.h>
-
- int FileSize(char *filename)
- {
- __aligned struct FileInfoBlock fib;
- BPTR fh;
- int size = -1;
-
- if(fh = Lock(filename, SHARED_LOCK))
- {
- if(Examine(fh, &fib) == DOSTRUE)
- {
- size = fib.fib_Size;
- }
- UnLock(fh);
- }
- return size;
- }
-
- The following version should work with any C compiler:
-
- #include <clib/dos_protos.h>
- #include <stdio.h>
-
- int FileSize(char *filename)
- {
- struct FileInfoBlock *fib;
- BPTR fh;
- int size = -1;
-
- fib = malloc(sizeof(struct FileInfoBlock));
- if(fib == NULL) return size;
-
- if(fh = Lock(filename, SHARED_LOCK))
- {
- if(Examine, fh, fib) == DOSTRUE)
- {
- size = fib->fib_Size;
- }
- UnLock(fh);
- }
- return size;
- }
-
- Your single read command is an fread(). Just allocate a buffer the
- size you need (using malloc()) and then fread() into the buffer.
-
- --
- ***** / walker@unx.sas.com
- *|_o_o|\\ Doug Walker< BIX, Portal: djwalker
- *|. o.| || \ CompuServe: 71165,2274
- | o |//
- ======
- Any opinions expressed are mine, not those of SAS Institute, Inc.
-